//
//  RHMEDataMonitorVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/2/27.
//

#import "RHMEDataMonitorVC.h"
#import "JXCategoryView.h"
#import "RHDeviceDataController.h"
#import "JXCategoryListContainerView.h"


#import "RHInputVC.h"
#import "RHOutputVC.h"
#import "RHMEDevEMSVC.h"
#import "RHBlockPCSParaVC.h"
#import "RHEMSDetailVC.h"
#import "RHElectricityMeterVC.h"
#import "RHMEDevLoadVC.h"
#import "RHMECustomVC.h"
#import "RHEventDetailVC.h"
#import "RHParaSelectVC.h"

@interface RHMEDataMonitorVC ()<JXCategoryListContainerViewDelegate,JXCategoryViewDelegate,JXCategoryTitleViewDataSource>

@property (nonatomic, strong) NSArray *titles;

@property (nonatomic, strong) JXCategoryTitleView *categoryView;

@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;

@property (nonatomic, strong) RHMECustomVC *cumstomVC;
@end

@implementation RHMEDataMonitorVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.deviceModel.deviceName;
    [self.view setBackgroundColor:CFTabViewBgColor];
    
//    self.titles = @[@"Input",@"Output",@"Battery",@"Ammeter",@"Load",@"Event",@"Cumstom"];
    //@"自定义".rh_localized
    self.titles = @[@"能量组".rh_localized,@"电表".rh_localized,@"负载".rh_localized];
    
    [self setupUI];
}


- (void)setupUI
{
    
    _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    [self.view addSubview:self.listContainerView];
    
    _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, 50)];
    self.categoryView.titles = self.titles;
    self.categoryView.backgroundColor = [UIColor whiteColor];
    self.categoryView.delegate = self;
//    self.categoryView.cellWidth = (SCREEN_WIDTH-30)/2;
    self.categoryView.titleSelectedColor = kCOLOR_HEX(@"323233");
    self.categoryView.titleColor = kCOLOR_HEX(@"969799");
    self.categoryView.titleFont = [UIFont systemFontOfSize:14];
    self.categoryView.titleSelectedFont = [UIFont boldSystemFontOfSize:14];
    self.categoryView.titleColorGradientEnabled = YES;
    self.categoryView.titleLabelZoomEnabled = NO;
    self.categoryView.titleLabelZoomScale = 1.0;
    self.categoryView.cellSpacing = 20;
    self.categoryView.contentEdgeInsetLeft = 15;
    self.categoryView.contentEdgeInsetRight = 15;
    self.categoryView.titleLabelVerticalOffset = 0;
    [self.view addSubview:self.categoryView];
    self.categoryView.listContainer = self.listContainerView;
    
    //下划线
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorColor = CFMainColor;
    lineView.indicatorWidth = JXCategoryViewAutomaticDimension;
   
    self.categoryView.indicators = @[lineView];
    
//    JXCategoryIndicatorBackgroundView *backgroundView = [[JXCategoryIndicatorBackgroundView alloc] init];
//     backgroundView.size = CGSizeMake(60, 3);
//     backgroundView.indicatorColor = CFMainColor;
//     backgroundView.indicatorHeight = 3;
//     backgroundView.indicatorWidth = 60;
//     backgroundView.verticalMargin = -10;
//     backgroundView.indicatorCornerRadius = 1.5;
//
//     _categoryView.indicators = @[backgroundView];
    
    
    [_categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(0);
        make.height.mas_equalTo(50);
    }];
    
    [_listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(self.categoryView.mas_bottom).offset(0);
        make.bottom.mas_offset(0);
    }];
    
   
}

#pragma mark ----- 电池详情 --------
-(void)batteryDetail{
    RHEMSDetailVC *detailVC = [[RHEMSDetailVC alloc]init];
   
    [self.navigationController pushViewController:detailVC animated:YES];
}

//参数选择
-(void)setPara{
    kWeakSelf;
    RHParaSelectVC *configVC = [[RHParaSelectVC alloc]init];
    configVC.selectParaBlock = ^(NSArray * _Nonnull paraArr) {
        weakSelf.cumstomVC.paraListArr = paraArr;
    };
    [self.navigationController pushViewController:configVC animated:YES];
}


#pragma mark - JXCategoryViewDelegate

// 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，不关心具体是点击还是滚动选中的。
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    NSLog(@"%@", NSStringFromSelector(_cmd));
    // 侧滑手势处理
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
    
    if(index == 0){
        UIButton *detailBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,40, 64)];
        [detailBtn setTitle:@"详情".rh_localized  forState:UIControlStateNormal];
        [detailBtn setBackgroundColor:UIColor.clearColor];
        [detailBtn setTitleColor:[UIColor colorWithHexString:@"3F4443" alpha:0.6] forState:UIControlStateNormal];
        [detailBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
//        [detailBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        [detailBtn addTarget:self action:@selector(batteryDetail) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:detailBtn];
//        self.navigationItem.rightBarButtonItem = rightBarBtn;
    }else if(index == 3){
        //参数选择
        UIButton *paraBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,40, 64)];
        [paraBtn setImage:[UIImage imageNamed:@"device_set_icon"] forState:UIControlStateNormal];
        [paraBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        [paraBtn addTarget:self action:@selector(setPara) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:paraBtn];
        self.navigationItem.rightBarButtonItem = rightBarBtn;
    }else{
        self.navigationItem.rightBarButtonItem = nil;
    }
}

// 滚动选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

#pragma mark - JXCategoryListContainerViewDelegate
// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return  self.titles.count;
}

// 返回各个列表菜单下的实例，该实例需要遵守并实现 <JXCategoryListContentViewDelegate> 协议
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {

//    if(index == 0){
//        RHInputVC *vc = [[RHInputVC alloc]init];
//        vc.deviceModel = self.deviceModel;
//        return vc;
//    }else if(index == 1){
//        RHOutputVC *vc = [[RHOutputVC alloc]init];
//        vc.deviceModel = self.deviceModel;
//        return vc;
//    }else
    if(index == 0){
        RHMEDevEMSVC *emsVC = [[RHMEDevEMSVC alloc]init];
        emsVC.deviceModel = self.deviceModel;
        return emsVC;
    }else if(index == 1){
        RHElectricityMeterVC *eleVC = [[RHElectricityMeterVC alloc]init];
        eleVC.deviceModel = self.deviceModel;
        return eleVC;
    }else if(index == 2){
        RHMEDevLoadVC *loadVC = [[RHMEDevLoadVC alloc]init];
        loadVC.deviceModel = self.deviceModel;
        return loadVC;
    }else if(index == 5){
        return [[RHEventDetailVC alloc]init];
    }else if(index == 3){
        RHMECustomVC *customVC = [[RHMECustomVC alloc]init];
        customVC.deviceModel = self.deviceModel;
        self.cumstomVC = customVC;
        return customVC;
    }
    
    
    
    return nil;
}


@end
